package com.cn.web.controller.Equipment;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.dto.BusinessProjectParameterItemDto;
import com.cn.bean.dto.ComponentSingleRegistrationDto;
import com.cn.bean.dto.SingleRegistrationTableDto;
import com.cn.bean.pojo.ComponentMechanical.EquipmentMechanicalStructureMappingTable;
import com.cn.bean.pojo.lbj.*;
import com.cn.bean.pojo.yw.BusinessProjectSingleParameterItemCurrentDataTable;
import com.cn.bean.vo.Equipment.ComponentBasicInformation;
import com.cn.bean.vo.Equipment.EquipmentStructure;
import com.cn.config.config.Result;
import com.cn.service.server.Equipment.SingleRegistrationTableService;
import com.cn.service.server.Equipment.SingleUpdateStatusService;
import com.cn.service.server.PartsManagement.ComponentSingleRegistrationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@Slf4j
@Transactional
public class SingleRegistrationTableController {
    @Autowired
    private SingleRegistrationTableService singleRegistrationTableService;
    @Autowired
    private ComponentSingleRegistrationService componentSingleRegistrationService;
    @Autowired
    private SingleUpdateStatusService singleUpdateStatusService;


    @RequestMapping("/select/SingleRegistrationTable")
    @ResponseBody
    public List<SingleRegistrationTable> selectMain(@RequestBody SingleRegistrationTable singleRegistrationTable) {
        List<SingleRegistrationTable> assists = singleRegistrationTableService.selectMain(singleRegistrationTable);
        return assists;
    }

    @RequestMapping("/select/selectSingleSub")
    @ResponseBody
    public List<SingleRegistrationTableSub> selectSingleSub(@RequestBody SingleRegistrationTableSub sub) {
        int a = singleUpdateStatusService.updateCurrent(sub.getId());
        List<SingleRegistrationTableSub> assists = singleRegistrationTableService.selectSingleSub(sub);
        return assists;
    }

    @RequestMapping("/select/EquipmentStructure")
    @ResponseBody
    public List<EquipmentStructure> selectEquipmentStructure(@RequestBody EquipmentStructure equipmentStructure) {
        List<EquipmentStructure> equipmentStructures = singleRegistrationTableService.selectEquipmentStructure(equipmentStructure);
        return equipmentStructures;
    }

    @RequestMapping("/select/ComponentBasicInformationTable")
    @ResponseBody
    public List<ComponentBasicInformation> selectComponentBasicInformationTable(@RequestBody ComponentBasicInformation componentBasicInformation) {
        List<ComponentBasicInformation> equipmentStructures = singleRegistrationTableService.selectComponentBasicInformation(componentBasicInformation);
        return equipmentStructures;
    }

    @RequestMapping("/select/EquipmentMechanicalStructureMapping")
    @ResponseBody
    public List<EquipmentMechanicalStructureMappingTable> selectEquipmentMechanicalStructureMappingTable(@RequestBody EquipmentMechanicalStructureMappingTable equipmentMechanicalStructureMappingTable) {
        List<EquipmentMechanicalStructureMappingTable> equipmentStructures = singleRegistrationTableService.selectEquipmentMappingTable(equipmentMechanicalStructureMappingTable);
        return equipmentStructures;
    }

    @RequestMapping("/select/SingleRegistrationTableSub")
    @ResponseBody
    public List<SingleRegistrationTableSub> selectSub(@RequestBody SingleRegistrationTableSub sub) {
        List<SingleRegistrationTableSub> selectSub = singleRegistrationTableService.selectSub(sub);
        return selectSub;
    }

    @RequestMapping("/insert/SingleRegistration")
    @ResponseBody
    public Result insertSingleRegistration(@RequestBody SingleRegistrationTableDto singleRegistrationTableDto) {
//        DataStructureDto dataStructureDto = jsonObject.getObject("requestData", DataStructureDto.class);
//        JSONArray dataArray = jsonObject.getJSONArray("requestData");
//        log.debug("dataStructureDto.getChineseName="+dataStructureDto.getChineseName());
        SingleRegistrationTable mainTable = new SingleRegistrationTable();
        SingleRegistrationTableSub subTable = new SingleRegistrationTableSub();
        SingleParameterItemCurrentDataTable currentDataTable = new SingleParameterItemCurrentDataTable();
        SingleParameterItemHistoryDataTable historyTable = new SingleParameterItemHistoryDataTable();
        ComponentSingleRegistrationTable componentSingle = new ComponentSingleRegistrationTable();
        BeanUtils.copyProperties(singleRegistrationTableDto, mainTable);
        BeanUtils.copyProperties(singleRegistrationTableDto, subTable);
        BeanUtils.copyProperties(singleRegistrationTableDto, currentDataTable);
        BeanUtils.copyProperties(singleRegistrationTableDto, historyTable);
//        Integer inputType = jsonObject.getInteger("inputType");
//        Integer materialStatus = jsonObject.getInteger("materialStatus");
//        Integer circulationState = jsonObject.getInteger("circulationState");
//        Integer lock = jsonObject.getInteger("lock");
        Integer id = singleRegistrationTableService.getSingleRegistrationTableAutoid();//主表id
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        id = id != null ? ++id : 1;
        Integer autoid = singleRegistrationTableService.getSubAutoid();//子表id
        autoid = autoid != null ? ++autoid : 1;
        Integer djid = componentSingleRegistrationService.getComponentSingleRegistrationAutoid();
        djid = djid != null ? ++djid : 1;
        if (singleRegistrationTableDto.getDjStatus() == 1) {
            return Result.failure("505");
        }
        //获取单件编号
        SingleRegistrationTableSub result = singleRegistrationTableService.selectPartSingleId(singleRegistrationTableDto.getPartSingleId());
        if (result != null && singleRegistrationTableDto.getPartSingleId().equals(result.getPartSingleId())) {
            return Result.failure("501");
        }
        mainTable.setId(id);
        mainTable.setDjNumber(String.valueOf(id));
        mainTable.setDjDate(format.format(date));
        mainTable.setCjsj(format.format(date));
        mainTable.setIsdel(0);
        subTable.setId(autoid);
        subTable.setMid(id);
        subTable.setPartSingleId(singleRegistrationTableDto.getPartSingleId());
        subTable.setCloseStatus(0);
        subTable.setSupplierUniqueCode(singleRegistrationTableDto.getSupplierUniqueCode());
        componentSingle.setId(djid);
        componentSingle.setComponentId(singleRegistrationTableDto.getBusinessProjectId());


        componentSingle.setCirculationState(singleRegistrationTableDto.getCirculationState());//流转状态
        componentSingle.setMaterialStatus(singleRegistrationTableDto.getMaterialStatus());//物料状态
        componentSingle.setLock(singleRegistrationTableDto.getLock());//锁定状态

        componentSingle.setSupplierUniqueCode(singleRegistrationTableDto.getSupplierUniqueCode());
        componentSingle.setUniqueCode(singleRegistrationTableDto.getPartSingleId());
        componentSingle.setMaterialCode(singleRegistrationTableDto.getWlbm());
        componentSingle.setEquipmentStructureId(singleRegistrationTableDto.getEquipmentStructureId());
        componentSingle.setEquipmentId(mainTable.getEquipmentId());
        componentSingle.setQuantity(subTable.getQuantity());
        currentDataTable.setId(autoid);
        currentDataTable.setPartSingleID(singleRegistrationTableDto.getPartSingleId());
        currentDataTable.setDateType(0);
        historyTable.setId(autoid);
        historyTable.setDateType(0);
        historyTable.setEquipmentStructureID(singleRegistrationTableDto.getEquipmentStructureId());
        historyTable.setBusinessProjectID(singleRegistrationTableDto.getBusinessProjectId());
        historyTable.setPartSingleID(String.valueOf(autoid));
        historyTable.setDateSerialNumber(autoid);
//        String userName = jsonObject.getString("userName");
        int a = singleRegistrationTableService.insertSingleRegistrationTable(mainTable);
        int b = singleRegistrationTableService.insertSingleRegistrationTableSub(subTable);
        int c = singleRegistrationTableService.insertCurrentDataTable(currentDataTable);
        int d = componentSingleRegistrationService.insertComponentSingleRegistration(componentSingle);
        int e = singleRegistrationTableService.insertHistoryDataTable(historyTable);
        if (a != 1 && b != 1 && c != 1 && e != 1 && d != 1) {
            return Result.failure("500");
        }
        return Result.success(200);
    }

    @RequestMapping("/insert/SingleRegistrationTable")
    @ResponseBody
    public Result insertSingleRegistrationTable(@RequestBody JSONObject jsonObject) {
        Integer materialStatus = jsonObject.getInteger("materialStatus");
        Integer circulationState = jsonObject.getInteger("circulationState");
        Integer lock = jsonObject.getInteger("lock");
//        DataStructureDto dataStructureDto = jsonObject.getObject("requestData", DataStructureDto.class);
        JSONArray dataArray = jsonObject.getJSONArray("requestData");
//        log.debug("dataStructureDto.getChineseName="+dataStructureDto.getChineseName());
        SingleRegistrationTable mainTable = new SingleRegistrationTable();
        Integer id = singleRegistrationTableService.getSingleRegistrationTableAutoid();
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        id = id != null ? ++id : 1;
        String userName = jsonObject.getString("userName");
        int categoryOfConsumables = jsonObject.getInteger("categoryOfConsumables");
        int equipmentStructureId = jsonObject.getInteger("equipmentStructureId");
        int businessProjectId = jsonObject.getInteger("businessProjectId");
        int djStatus = jsonObject.getInteger("djStatus");
        int equipmentId = jsonObject.getInteger("equipmentId");
        if (djStatus == 1) {
            return Result.failure("505");
        }

        mainTable.setId(id);
        mainTable.setDjNumber(String.valueOf(id));
        mainTable.setBusinessProjectId(businessProjectId);
        mainTable.setCategoryOfConsumables(categoryOfConsumables);
        mainTable.setDjStatus(djStatus);
        mainTable.setEquipmentStructureId(equipmentStructureId);
        mainTable.setDjDate(format.format(date));
        mainTable.setCjsj(format.format(date));
        mainTable.setEquipmentId(equipmentId);
        mainTable.setIsdel(0);
        int a = singleRegistrationTableService.insertSingleRegistrationTable(mainTable);
        if (a != 1) {
            return Result.failure("500");
        }
        if (dataArray != null && !dataArray.isEmpty()) {
            List<SingleRegistrationTableDto> djBhList = dataArray.toJavaList(SingleRegistrationTableDto.class);
            for (SingleRegistrationTableDto dataStructureDto : djBhList) {
                SingleRegistrationTableSub subTable = new SingleRegistrationTableSub();
                SingleParameterItemCurrentDataTable currentDataTable = new SingleParameterItemCurrentDataTable();
                SingleParameterItemHistoryDataTable historyTable = new SingleParameterItemHistoryDataTable();
                ComponentSingleRegistrationTable componentSingle = new ComponentSingleRegistrationTable();
                BeanUtils.copyProperties(dataStructureDto, subTable);
                BeanUtils.copyProperties(dataStructureDto, currentDataTable);
                BeanUtils.copyProperties(dataStructureDto, historyTable);
                Integer autoid = singleRegistrationTableService.getSubAutoid();
                autoid = autoid != null ? ++autoid : 1;
                subTable.setId(autoid);
                subTable.setMid(id);
                subTable.setCloseStatus(1);
                subTable.setPartSingleId(String.valueOf(autoid));
                subTable.setSupplierUniqueCode(String.valueOf(autoid));
                currentDataTable.setDateType(0);
                currentDataTable.setId(autoid);
                currentDataTable.setPartSingleID(String.valueOf(autoid));
                componentSingle.setId(autoid);
//                componentSingle.setComponentId(businessProjectId);
//                componentSingle.setCirculationState(circulationState);
//                componentSingle.setMaterialStatus(materialStatus);
                componentSingle.setLock(lock);
                componentSingle.setEquipmentId(equipmentId);
                componentSingle.setSupplierUniqueCode(dataStructureDto.getSupplierUniqueCode());
                componentSingle.setUniqueCode(String.valueOf(autoid));
                componentSingle.setMaterialCode(dataStructureDto.getWlbm());
                componentSingle.setEquipmentStructureId(equipmentStructureId);
                componentSingle.setEquipmentId(mainTable.getEquipmentId());
                historyTable.setId(autoid);
                historyTable.setDateType(0);
                historyTable.setPartSingleID(String.valueOf(autoid));
                historyTable.setBusinessProjectID(businessProjectId);
                historyTable.setCategoryOfConsumables(categoryOfConsumables);
                historyTable.setEquipmentStructureID(equipmentStructureId);
                historyTable.setDateSerialNumber(autoid);
                int b = singleRegistrationTableService.insertSingleRegistrationTableSub(subTable);
                int c = singleRegistrationTableService.insertCurrentDataTable(currentDataTable);
                int e = singleRegistrationTableService.insertHistoryDataTable(historyTable);
                int d = componentSingleRegistrationService.insertComponentSingleRegistration(componentSingle);
//                int c = componentSingleRegistrationService.insertHistoryDataTable(historyTable);
                if (b != 1 && c != 1 && e != 1 && d != 1) {
                    return Result.failure("500");
                }
            }
        }

        return Result.success(200);
    }

    @RequestMapping("/update/SingleRegistration/abc")
    @ResponseBody
    public Result updateSingleRegistration(@RequestBody SingleRegistrationTableDto singleRegistrationTableDto, @RequestBody JSONObject jsonObject) {
//        JSONArray dataArray = jsonObject.getJSONArray("requestData");
//        log.debug("dataStructureDto.getChineseName="+dataStructureDto.getChineseName());
        Integer materialStatus = jsonObject.getInteger("materialStatus");
        Integer circulationState = jsonObject.getInteger("circulationState");
        Integer lock = jsonObject.getInteger("lock");
        SingleRegistrationTable mainTable = new SingleRegistrationTable();
        SingleRegistrationTableSub subTable = new SingleRegistrationTableSub();
        SingleParameterItemCurrentDataTable currentDataTable = new SingleParameterItemCurrentDataTable();
        SingleParameterItemHistoryDataTable historyTable = new SingleParameterItemHistoryDataTable();
        BeanUtils.copyProperties(singleRegistrationTableDto, mainTable);
        BeanUtils.copyProperties(singleRegistrationTableDto, subTable);
        BeanUtils.copyProperties(singleRegistrationTableDto, currentDataTable);
        BeanUtils.copyProperties(singleRegistrationTableDto, historyTable);
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Integer autoid = singleRegistrationTableService.getSubAutoid();
        autoid = autoid != null ? ++autoid : 1;
        subTable.setId(autoid);
        subTable.setMid(singleRegistrationTableDto.getId());
        subTable.setCloseStatus(1);
        subTable.setPartSingleId(singleRegistrationTableDto.getPartSingleId());
        subTable.setSupplierUniqueCode(singleRegistrationTableDto.getSupplierUniqueCode());

        //                componentSingle.setComponentId(businessProjectId);
//                componentSingle.setCirculationState(circulationState);
//                componentSingle.setMaterialStatus(materialStatus);

        currentDataTable.setDateType(1);
        currentDataTable.setId(autoid);
        currentDataTable.setPartSingleID(String.valueOf(autoid));
        historyTable.setId(autoid);
        historyTable.setDateType(1);
        historyTable.setEquipmentStructureID(singleRegistrationTableDto.getEquipmentStructureId());
        historyTable.setBusinessProjectID(singleRegistrationTableDto.getBusinessProjectId());
        historyTable.setPartSingleID(String.valueOf(autoid));

        int a = singleRegistrationTableService.updateMain(mainTable);
        if (a != 1) {
            return Result.failure("500", "更新主表失败");
        }
        List<SingleRegistrationTableSub> statusOperationDtos = singleRegistrationTableService.selectSingleRegistrationTableSub(singleRegistrationTableDto.getId());
        if (statusOperationDtos.size() > 0) {
            int sub = singleRegistrationTableService.delSub(singleRegistrationTableDto.getId());
        }

        int b = singleRegistrationTableService.insertSingleRegistrationTableSub(subTable);
        int c = singleRegistrationTableService.insertCurrentDataTable(currentDataTable);
        int e = singleRegistrationTableService.insertHistoryDataTable(historyTable);
        if (b != 1 && c != 1 && e != 1) {
            return Result.failure("500");
        }
        return Result.success(200);
    }

    @RequestMapping("/update/SingleRegistrationTable")
    @ResponseBody
    public Result updateSingleRegistrationTable(@RequestBody JSONObject jsonObject) {
        JSONArray dataArray = jsonObject.getJSONArray("requestData");
//        log.debug("dataStructureDto.getChineseName="+dataStructureDto.getChineseName());
        SingleRegistrationTable mainTable = new SingleRegistrationTable();
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int id = jsonObject.getInteger("id");
        String userName = jsonObject.getString("userName");
        String djNumber = jsonObject.getString("djNumber");
        String djDate = jsonObject.getString("djDate");
        int categoryOfConsumables = jsonObject.getInteger("categoryOfConsumables");
        int equipmentStructureId = jsonObject.getInteger("equipmentStructureId");
        int businessProjectId = jsonObject.getInteger("businessProjectId");
        int djStatus = jsonObject.getInteger("djStatus");
        mainTable.setId(id);
        mainTable.setDjNumber(djNumber);
        mainTable.setBusinessProjectId(businessProjectId);
        mainTable.setCategoryOfConsumables(categoryOfConsumables);
        mainTable.setDjStatus(djStatus);
        mainTable.setEquipmentStructureId(equipmentStructureId);
        mainTable.setDjDate(djDate);
        int a = singleRegistrationTableService.updateMain(mainTable);
        if (a != 1) {
            return Result.failure("500", "更新主表失败");
        }
        List<SingleRegistrationTableSub> statusOperationDtos = singleRegistrationTableService.selectSingleRegistrationTableSub(id);
        if (statusOperationDtos.size() > 0) {
            int sub = singleRegistrationTableService.delSub(id);
        }
        if (dataArray == null || dataArray.isEmpty()) {
            return Result.success(200);
        }
        List<SingleRegistrationTableDto> djBhList = dataArray.toJavaList(SingleRegistrationTableDto.class);
        for (SingleRegistrationTableDto dataStructureDto : djBhList) {
            SingleRegistrationTableSub subTable = new SingleRegistrationTableSub();
            SingleParameterItemCurrentDataTable currentDataTable = new SingleParameterItemCurrentDataTable();
            SingleParameterItemHistoryDataTable historyTable = new SingleParameterItemHistoryDataTable();
            BeanUtils.copyProperties(dataStructureDto, subTable);
            BeanUtils.copyProperties(dataStructureDto, currentDataTable);
            BeanUtils.copyProperties(dataStructureDto, historyTable);
            Integer autoid = singleRegistrationTableService.getSubAutoid();
            autoid = autoid != null ? ++autoid : 1;
            subTable.setId(autoid);
            subTable.setMid(id);
            subTable.setCloseStatus(1);
            subTable.setPartSingleId(String.valueOf(autoid));
            subTable.setSupplierUniqueCode(String.valueOf(autoid));
            currentDataTable.setDateType(1);
            currentDataTable.setId(autoid);
            currentDataTable.setPartSingleID(String.valueOf(autoid));
            historyTable.setId(autoid);
            historyTable.setDateType(1);
            historyTable.setPartSingleID(String.valueOf(autoid));
            historyTable.setBusinessProjectID(businessProjectId);
            historyTable.setCategoryOfConsumables(categoryOfConsumables);
            historyTable.setEquipmentStructureID(equipmentStructureId);
            historyTable.setDateSerialNumber(autoid);

            int b = singleRegistrationTableService.insertSingleRegistrationTableSub(subTable);
            int c = singleRegistrationTableService.insertCurrentDataTable(currentDataTable);
            int e = singleRegistrationTableService.insertHistoryDataTable(historyTable);
//                int c = componentSingleRegistrationService.insertHistoryDataTable(historyTable);
            if (b != 1 && c != 1 && e != 1) {
                return Result.failure("500");
            }
        }
        return Result.success(200);
    }

    @RequestMapping("/del/SingleRegistration")
    @ResponseBody
    public Result delSingleRegistration(@RequestBody JSONObject jsonObject) {
        JSONArray djBhArray = jsonObject.getJSONArray("id");
        if (djBhArray != null && !djBhArray.isEmpty()) {
            List<Integer> djBhList = djBhArray.toJavaList(Integer.class);
            for (Integer id : djBhList) {
                singleRegistrationTableService.delMain(id);
                singleRegistrationTableService.delSingleRegistrationTableSub(id);
            }
            return Result.success(200);
        }
        return Result.failure("500", "删除失败");
    }

    @RequestMapping("/audit/SingleRegistration")
    @ResponseBody
    public Result auditSingleRegistration(@RequestBody JSONObject jsonObject) {
        JSONArray djBhArray = jsonObject.getJSONArray("id");
        if (djBhArray != null && !djBhArray.isEmpty()) {
            List<Integer> djBhList = djBhArray.toJavaList(Integer.class);
            for (Integer id : djBhList) {
                singleRegistrationTableService.auditMain(id);
            }
            return Result.success(200);
        }
        return Result.failure("500", "删除失败");
    }

    @RequestMapping("/audit/SingleRegistration0")
    @ResponseBody
    public Result auditSingleRegistration0(@RequestBody JSONObject jsonObject) {
        JSONArray djBhArray = jsonObject.getJSONArray("id");
        if (djBhArray != null && !djBhArray.isEmpty()) {
            List<Integer> djBhList = djBhArray.toJavaList(Integer.class);
            for (Integer id : djBhList) {
                singleRegistrationTableService.auditMain0(id);
            }
            return Result.success(200);
        }
        return Result.failure("500", "删除失败");
    }
}
